package gobeshona.googleaccount;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public final class GoogleAccountServletFilter implements Filter{

	private static final Logger log = Logger.getLogger(GoogleAccountServletFilter.class.getName());
	
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
				
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		
		UserService userService = UserServiceFactory.getUserService();
        log.info("request.getRequestURI(): " + request.getRequestURI());
        if (request.getRequestURI().equals("/logout")){
        	log.info("logging out user");
        	response.sendRedirect(userService.createLogoutURL("/youareloggedout.html"));
        }
        if (request.getUserPrincipal() != null){
        	log.info("userService.isUserLoggedIn(): " + userService.isUserLoggedIn());
        	log.info("userService.getCurrentUser().getNickname()  " + userService.getCurrentUser().getNickname());
            log.info("userService.getCurrentUser().getEmail()  " + userService.getCurrentUser().getEmail());
            log.info("userService.getCurrentUser().getAuthDomain()  " + userService.getCurrentUser().getAuthDomain());
            log.info("request.getUserPrincipal().getName(): " + request.getUserPrincipal().getName());
            
            if (userService.getCurrentUser().getEmail().equals("yusuf.iqbal@yahoo.com")){
            	log.info("User logged in with authorized user. do chain");
            	chain.doFilter(request, response);
    		}else{
    			log.info("User is not a approved Google Account user. Let the user logout and login with appropriate Google account");
    			log.info("LogOut: " + userService.createLogoutURL(request.getRequestURI()));
    			response.getWriter().append("<html><body><p> " + userService.getCurrentUser().getEmail() +" is not Authorized. ");
    			response.getWriter().append("  You can <a href=\"" +  userService.createLogoutURL(request.getRequestURI()) + "\">sign out and sign back in with a authorized Google Account</a>.");
    			response.getWriter().append("</p></bdoy></html>");
    		}
        }
        else{
    		log.info("User is not logged in. Redirect.");
    		log.info("userService.createLoginURL(request.getRequestURI(): " + userService.createLoginURL(request.getRequestURI()) );
    		response.sendRedirect(userService.createLoginURL(request.getRequestURI()));
        }
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}
}
